package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.VideoManager;
import com.itheima.domain.db.User;
import com.itheima.vo.PageBeanVo;
import org.apache.catalina.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {

    @Autowired
    VideoManager videoManager;

    @GetMapping("/smallVideos")
    public ResponseEntity findRecommendVideoByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        //1.先获取用户的id
        Long userId = UserHolder.get().getId();

        //2.调用manager返回pageBeanVo
        PageBeanVo pageBeanVo = videoManager.findRecommendVideoByPage(userId, pageNum, pageSize);

        //3.返回pageBeanVo
        return ResponseEntity.ok(pageBeanVo);
    }

    @PostMapping("/smallVideos")
    public void save(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        //2.获取当前登陆者id
        Long userId = UserHolder.get().getId();
        //3.调用manager完成保存
        videoManager.save(userId, videoThumbnail, videoFile);
    }

    //关注
    @PostMapping("/smallVideos/{focusUserId}/userFocus")
    public void focusUser(@PathVariable Long focusUserId) {
        videoManager.focusUser(focusUserId);
    }

    //取关
    @PostMapping("/smallVideos/{focusUserId}/userUnFocus")
    public void unFocusUser(@PathVariable Long focusUserId) {
        videoManager.unFocusUser(focusUserId);
    }


    //视频点赞
    @PostMapping("/smallVideos/{publishId}/like")
    public void videosLike(@PathVariable String publishId) {
        //获取用户id
        Long userId = UserHolder.get().getId();
        videoManager.videosLike(userId, publishId);
    }

    //视频取消点赞
    @PostMapping("/smallVideos/{publishId}/dislike")
    public void videoDisLike(@PathVariable String publishId){
        //获取用户id
        Long userId = UserHolder.get().getId();
        videoManager.videosDislike(userId, publishId);
    }



    //发布视频评论
    @PostMapping("/smallVideos/{publishId}/comments")
    public ResponseEntity videosComment(
            @PathVariable String publishId,
            @RequestBody Map<String,String> map
    ) {

        Long userId = UserHolder.get().getId();
        String content = map.get("comment");

        return videoManager.videosComment(publishId, content,userId);
    }

    /**
     * 查看评论列表
     */
    @GetMapping("/smallVideos/{videoId}/comments")
    public ResponseEntity findVideoCommentsByPage(
            @PathVariable String videoId,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize
    ) {

        Long userId = UserHolder.get().getId();
        PageBeanVo pageBeanVo = videoManager.findVideoCommentsByPage(videoId, pageNum, pageSize,userId);
        return ResponseEntity.ok(pageBeanVo);
    }

    /**
     * 评论点赞
     */
    @PostMapping("/smallVideos/comments/{publishId}/like")
    public ResponseEntity videoCoLike(@PathVariable String publishId) {
        Long userId = UserHolder.get().getId();
        return videoManager.videoCommentLike(publishId, userId);
    }

}
